package com.jiyun.demo;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class FindOne {
    public static void main(String[] args) {
        //创建链接
        MongoClient client = new MongoClient("127.0.0.1");
        //链接数据库
        MongoDatabase database = client.getDatabase("2108a");
        //获取集合
        MongoCollection<Document> user = database.getCollection("user");
        //构建查询条件
        BasicDBObject id = new BasicDBObject("_id", "2");
        //查询记录获取集合
        FindIterable<Document> documents = user.find(id);
        for (Document document : documents) {
            System.out.println("内容"+document.getString("content"));
            System.out.println("用户id"+document.getString("userid"));
            System.out.println("浏览量"+document.getInteger("visits"));
            System.out.println("==============");
        }
        client.close();
    }

}
